package com.food.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.food.user.dto.EsFoodDto;
import com.food.user.entity.FoodInfo;
import com.food.user.form.EsFoodForm;
import com.food.user.mapper.FoodInfoMapper;
import com.food.user.model.StoreFoodModel;
import com.food.user.service.FoodInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class FoodInfoServiceImpl extends ServiceImpl<FoodInfoMapper, FoodInfo> implements FoodInfoService {

    @Autowired
    private ElasticsearchRestTemplate ert;


    @Override
    public EsFoodDto esList(EsFoodForm form) {
        NativeSearchQueryBuilder nsqb=new NativeSearchQueryBuilder();
        BoolQueryBuilder bqb1=new BoolQueryBuilder();
        List<QueryBuilder> must = bqb1.must();
        if (form.getKeyword()!=null&&!form.getKeyword().equals("")){
            BoolQueryBuilder bqb2=new BoolQueryBuilder();
            List<QueryBuilder> should = bqb2.should();
            should.add(new MatchQueryBuilder("foodInfoFoodName",form.getKeyword()));
            should.add(new MatchQueryBuilder("foodInfoDetailInfo",form.getKeyword()));
            must.add(bqb2);
        }
        if (form.getTypeId()!=null){
            TermQueryBuilder typeId = new TermQueryBuilder("typeId", form.getTypeId());
            must.add(typeId);
        }
        TermQueryBuilder termQueryBuilder = new TermQueryBuilder("foodInfoStoreId", form.getFoodInfoStoreId());
        must.add(termQueryBuilder);
        nsqb.withQuery(bqb1);

        nsqb.withPageable(PageRequest.of(form.getPageIndex()-1,form.getPageSize()));


        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("foodInfoDetailInfo");
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        highlightBuilder.fragmentSize(30);
        highlightBuilder.numOfFragments(1);
        nsqb.withHighlightBuilder(highlightBuilder);

        NativeSearchQuery nsq=nsqb.build();
        SearchHits<StoreFoodModel> search = ert.search(nsq, StoreFoodModel.class);


        //组装dto
        EsFoodDto esReturnDto = new EsFoodDto();
        esReturnDto.setTotal(search.getTotalHits());
        esReturnDto.setPageIndex(form.getPageIndex());
        esReturnDto.setPageSize(form.getPageSize());
        for (SearchHit<StoreFoodModel> searchHit : search.getSearchHits()) {
            StoreFoodModel content = searchHit.getContent();
            if (searchHit.getHighlightFields()!=null&&searchHit.getHighlightFields().size()>0){
                if (searchHit.getHighlightFields().get("foodInfoDetailInfo")!=null){
                    content.setFoodInfoDetailInfo(searchHit.getHighlightFields().get("foodInfoDetailInfo").get(0));
                }
            }else {
                if (content.getFoodInfoDetailInfo().length()>=30){
                    content.setFoodInfoDetailInfo(content.getFoodInfoDetailInfo().substring(0,29));
                }
            }
            esReturnDto.getFoodModels().add(content);
        }
        return esReturnDto;
    }
}
